/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evolve.webbot.robot;

import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import static java.awt.event.KeyEvent.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;
import com.evolve.webbot.util.Utileria;
/**
 *
 * @author DarthAE
 */
public class RobotRlWalmart {
    
    private String path;
    private Robot robot = null;
    private SimpleDateFormat sdfFull= new SimpleDateFormat("yyyyMMdd");
    private SimpleDateFormat sdfDia= new SimpleDateFormat("dd");
    private SimpleDateFormat sdfMes= new SimpleDateFormat("MM");
    private SimpleDateFormat sdfAnio= new SimpleDateFormat("yyyy");
    
    public static String WB_MAIN_DIR = "evolve-webbot";
    public static String ROBOT_MAIN_DIR = "retail-webbot";
    public static String ROBOT_PROP_DIR = "retail-properties";
    public static String ROBOT_PROP_ARC = "retailPortal.properties";
    
    static Logger log = Logger.getLogger( RobotRlWalmart.class ); 
    
    public RobotRlWalmart(){}
    
    public RobotRlWalmart(String path){
        this.path = path;
    }
    
    public void startIexplorer()
    {
      Utileria util = new Utileria();
      String   iEpath="";
      String   urlRl ="";
      
      iEpath = util.getProperty("retailRobot.properties","Portal.iepath");
      urlRl  = util.getProperty("retailRobot.properties","Portal.urlrl");

      try{ 
              log.info("inicia ie, comando ejecutado : "+iEpath+" "+urlRl);
              Runtime runinstance = Runtime.getRuntime();
              runinstance.exec(iEpath+" "+urlRl);
          }catch(Exception e){
                log.info("error al iniciar ie "+e);}  
    }
    
    public void initRobot()
    {
       try{        
             robot = new Robot();      
          } catch(Exception e){log.info("inicia robot posicionamiento "+e); }      
    }
    
    public void moveCursorToPos()
    {     
        Date date = new Date();
        Utileria util = new Utileria();
        
        try{
            Thread.sleep(15000);
           }catch(Exception e){};
            
            /*inicio usuario*/
            robot.mouseMove(670, 357);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);  
            
            /*tecleo de usuario*/
            
            try{
                 Thread.sleep(2500);
               }catch(Exception e){};            
            type(util.getProperty("retailRobot.properties","Portal.user"));
            try{
                 Thread.sleep(2500);
               }catch(Exception e){};
            /*fin tecleo de usuario*/
            /*tecleo pssword*/
            
            
            try{
                 Thread.sleep(2500);
               }catch(Exception e){};
            /*fin tecleo password*/
               
            robot.mouseMove(670, 387);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);  
            type(util.getProperty("retailRobot.properties","Portal.passwd"));
            /*tecleo password*/            
            
        try{
            Thread.sleep(15000);
           }catch(Exception e){};  
           
            /*inicio boton logon*/
            robot.mouseMove(670, 521);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);  
            
        try{
            Thread.sleep(15000);
           }catch(Exception e){}; 

           /*inicio times*/
           //se mueve al menu times
            robot.mouseMove(390,96);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            
        try{
            Thread.sleep(15000);
           }catch(Exception e){}; 
           
           //se mueve al menut time ranges 1
           
            robot.mouseMove(43,171);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK); 
            
        try{
            Thread.sleep(15000);
           }catch(Exception e){}; 
 
            robot.mouseMove(280,249);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);  
 
        try{
            Thread.sleep(15000);
           }catch(Exception e){}; 
 
            robot.mouseMove(336,266);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
 
        try{
            Thread.sleep(15000);
           }catch(Exception e){}; 
 
            robot.mouseMove(796,369);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
        
        try{
            Thread.sleep(15000);
           }catch(Exception e){}; 
 
            robot.mouseMove(977,424);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK); 
            
        try{
            Thread.sleep(15000);
           }catch(Exception e){}; 
 
            robot.mouseMove(742,264);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK); 
        
            try{
            Thread.sleep(15000);
           }catch(Exception e){};             
            
           /*teclea fecha 01*/
           type(sdfMes.format(date));

            try{
            Thread.sleep(15000);
           }catch(Exception e){}; 
           
           type(sdfDia.format(date)); 
           
           try{
            Thread.sleep(15000);
           }catch(Exception e){};     
           
           type(sdfAnio.format(date)); 
           
           try{
            Thread.sleep(15000);
           }catch(Exception e){};
           
            robot.mouseMove(742,312);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);           
           
           try{
            Thread.sleep(15000);
           }catch(Exception e){};            
            
           /*teclea fecha 02*/
           type(sdfMes.format(date));

            try{
            Thread.sleep(15000);
           }catch(Exception e){}; 
           
           type(sdfDia.format(date)); 
           
           try{
            Thread.sleep(15000);
           }catch(Exception e){};     
           
           type(sdfAnio.format(date));            
           
           try{
            Thread.sleep(15000);
           }catch(Exception e){};            
           
            robot.mouseMove(737,341);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK); 
            
           /*termino times*/
            
           try{
            Thread.sleep(15000);
           }catch(Exception e){};               
           
           /*inicia submit*/
           
            robot.mouseMove(608,96);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            
           try{
            Thread.sleep(15000);
           }catch(Exception e){}; 
           
            robot.mouseMove(256,176);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            
           try{
            Thread.sleep(15000);
           }catch(Exception e){}; 
           
//           for(int i=0;i<30;i++)
//           {
//               robot.mousePress(KeyEvent.VK_SPACE);
//               robot.mouseRelease(KeyEvent.VK_SPACE);
//               
//               try{
//                    Thread.sleep(2000);
//                   }catch(Exception e){};
//           }
           
           type(sdfFull.format(date));

        try{
            Thread.sleep(15000);
           }catch(Exception e){}; 
           
            robot.mouseMove(354,180);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK); 
            
           /*termina submit*/
        try{
            Thread.sleep(15000);
           }catch(Exception e){};           
           
            /*termina ie*/
            robot.mouseMove(1350,11);  
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);             
    }
    
    public void type(CharSequence characters) {
        int length = characters.length();
        for (int i = 0; i < length; i++) {
            char character = characters.charAt(i);
            type(character);
        }
    }
    
    public void type(char character) 
    {
        switch (character) {
        case 'a': doType(VK_A); break;
        case 'b': doType(VK_B); break;
        case 'c': doType(VK_C); break;
        case 'd': doType(VK_D); break;
        case 'e': doType(VK_E); break;
        case 'f': doType(VK_F); break;
        case 'g': doType(VK_G); break;
        case 'h': doType(VK_H); break;
        case 'i': doType(VK_I); break;
        case 'j': doType(VK_J); break;
        case 'k': doType(VK_K); break;
        case 'l': doType(VK_L); break;
        case 'm': doType(VK_M); break;
        case 'n': doType(VK_N); break;
        case 'o': doType(VK_O); break;
        case 'p': doType(VK_P); break;
        case 'q': doType(VK_Q); break;
        case 'r': doType(VK_R); break;
        case 's': doType(VK_S); break;
        case 't': doType(VK_T); break;
        case 'u': doType(VK_U); break;
        case 'v': doType(VK_V); break;
        case 'w': doType(VK_W); break;
        case 'x': doType(VK_X); break;
        case 'y': doType(VK_Y); break;
        case 'z': doType(VK_Z); break;
        case 'A': doType(VK_SHIFT, VK_A); break;
        case 'B': doType(VK_SHIFT, VK_B); break;
        case 'C': doType(VK_SHIFT, VK_C); break;
        case 'D': doType(VK_SHIFT, VK_D); break;
        case 'E': doType(VK_SHIFT, VK_E); break;
        case 'F': doType(VK_SHIFT, VK_F); break;
        case 'G': doType(VK_SHIFT, VK_G); break;
        case 'H': doType(VK_SHIFT, VK_H); break;
        case 'I': doType(VK_SHIFT, VK_I); break;
        case 'J': doType(VK_SHIFT, VK_J); break;
        case 'K': doType(VK_SHIFT, VK_K); break;
        case 'L': doType(VK_SHIFT, VK_L); break;
        case 'M': doType(VK_SHIFT, VK_M); break;
        case 'N': doType(VK_SHIFT, VK_N); break;
        case 'O': doType(VK_SHIFT, VK_O); break;
        case 'P': doType(VK_SHIFT, VK_P); break;
        case 'Q': doType(VK_SHIFT, VK_Q); break;
        case 'R': doType(VK_SHIFT, VK_R); break;
        case 'S': doType(VK_SHIFT, VK_S); break;
        case 'T': doType(VK_SHIFT, VK_T); break;
        case 'U': doType(VK_SHIFT, VK_U); break;
        case 'V': doType(VK_SHIFT, VK_V); break;
        case 'W': doType(VK_SHIFT, VK_W); break;
        case 'X': doType(VK_SHIFT, VK_X); break;
        case 'Y': doType(VK_SHIFT, VK_Y); break;
        case 'Z': doType(VK_SHIFT, VK_Z); break;
        case '`': doType(VK_BACK_QUOTE); break;
        case '0': doType(VK_0); break;
        case '1': doType(VK_1); break;
        case '2': doType(VK_2); break;
        case '3': doType(VK_3); break;
        case '4': doType(VK_4); break;
        case '5': doType(VK_5); break;
        case '6': doType(VK_6); break;
        case '7': doType(VK_7); break;
        case '8': doType(VK_8); break;
        case '9': doType(VK_9); break;
        case '-': doType(VK_MINUS); break;
        case '=': doType(VK_EQUALS); break;
        case '~': doType(VK_SHIFT, VK_BACK_QUOTE); break;
        /*case '!': doType(VK_EXCLAMATION_MARK); break;*/
        case '!':doType(VK_SHIFT, VK_1);break;
        case '@': doType(VK_AT); break;
        case '#': doType(VK_NUMBER_SIGN); break;
        case '$': doType(VK_DOLLAR); break;
        case '%': doType(VK_SHIFT, VK_5); break;
        case '^': doType(VK_CIRCUMFLEX); break;
        case '&': doType(VK_AMPERSAND); break;
        case '*': doType(VK_ASTERISK); break;
        case '(': doType(VK_LEFT_PARENTHESIS); break;
        case ')': doType(VK_RIGHT_PARENTHESIS); break;
        //case '_': doType(VK_UNDERSCORE); break;
        case '_': doType(VK_SHIFT,VK_MINUS); break;
        case '+': doType(VK_PLUS); break;
        case '\t': doType(VK_TAB); break;
        case '\n': doType(VK_ENTER); break;
        case '[': doType(VK_OPEN_BRACKET); break;
        case ']': doType(VK_CLOSE_BRACKET); break;
        case '\\': doType(VK_BACK_SLASH); break;
        case '{': doType(VK_SHIFT, VK_OPEN_BRACKET); break;
        case '}': doType(VK_SHIFT, VK_CLOSE_BRACKET); break;
        case '|': doType(VK_SHIFT, VK_BACK_SLASH); break;
        case ';': doType(VK_SEMICOLON); break;
        case ':': doType(VK_COLON); break;
        case '\'': doType(VK_QUOTE); break;
        case '"': doType(VK_QUOTEDBL); break;
        case ',': doType(VK_COMMA); break;
        case '<': doType(VK_LESS); break;
        case '.': doType(VK_PERIOD); break;
        case '>': doType(VK_GREATER); break;
        case '/': doType(VK_SLASH); break;
        case '?': doType(VK_SHIFT, VK_SLASH); break;
        case ' ': doType(VK_SPACE); break;
        default:
            throw new IllegalArgumentException("Cannot type character " + character);
        }
    }  
    
    private void doType(int... keyCodes) {
        doType(keyCodes, 0, keyCodes.length);
    }

    private void doType(int[] keyCodes, int offset, int length) {
        if (length == 0) {
            return;
        }

        robot.keyPress(keyCodes[offset]);
        doType(keyCodes, offset + 1, length - 1);
        robot.keyRelease(keyCodes[offset]);
    }        
}
